import Mock from 'mockjs'

/**
 * AuthMenus 模拟数据
 */
class MenusMock {

    // 显示资源选项
    option(option) {
        let param = this.parseOption(option);
        let data = {
            options_of_status: { 0: '待审核', 1: '启用', 2: '禁用' },
            options_of_type: { 1: '目录', 2: '页面', 3: '操作' }
        }
        return this.stdMessage(param._success, '没有数据', data);
    }

    // 显示资源列表
    index(option) {
        let param = this.parseOption(option);
        console.log('[菜单模拟数据] 显示资源列表 param=', param, '&option=', option);
        let created_at = Mock.mock('@now'), status_text = '正常', sort = 0;
        // 可以通过传参 _success 来控制返回消息的成功状态。
        let data = [
            {
                id: 10,
                title: "教室管理",
                children: [
                    {
                        id: 11, title: "教室管理", path: "classroom",
                        children: [
                            { id: 111, title: '列表', code: 'index' }
                        ]
                    },

                ],
            },
            {
                id: 20,
                title: "教学监控",
                children: [
                    {
                        id: 21, title: "教学监控", path: "monitor",
                        children: [
                            { id: 211, title: '列表', code: 'index' }
                        ]
                    },

                ],
            },
            {
                id: 30,
                title: "教学联动",
                children: [
                    {
                        id: 31, title: "学期管理",  path: "semester",
                        children: [
                            { id: 311, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 32, title: "节次管理",  path: "timetable",
                        children: [
                            { id: 321, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 33, title: "课表管理",  path: "schedule",
                        children: [
                            { id: 331, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 34, title: "任务管理",  path: "task",
                        children: [
                            { id: 341, title: '列表', code: 'index' }
                        ]
                    },

                ],
            },
        

            {
                id: 40,
                title: "运维管理",
                children: [
                    {
                        id: 41, title: "智能巡检",  path: "patrollog",
                        children: [
                            { id: 411, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 42, title: "故障报修",  path: "workerorder",
                        children: [
                            { id: 421, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 43, title: "操作日志",  path: "control",
                        children: [
                            { id: 431, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 44, title: "刷卡日志",  path: "cardlog",
                        children: [
                            { id: 441, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 45, title: "远程协助",  path: "index",
                        children: [
                            { id: 451, title: '列表', code: 'index' }
                        ]
                    },
                ],
            },


            {
                id: 50,
                title: "资产管理",
                children: [
                    {
                        id: 51, title: "资产管理", path: "equipment",
                        children: [
                            { id: 511, title: '列表', code: 'index' }
                        ]
                    },

                ],
            },
            {
                id: 60,
                title: "数据统计",
                children: [
                    {
                        id: 61, title: "报表统计", path: "report",
                        children: [
                            { id: 611, title: '列表', code: 'index' }
                        ]
                    },
                    {
                        id: 62, title: "数据大屏", path: "bigscreen",
                        children: [
                            { id: 621, title: '列表', code: 'index' }
                        ]
                    },
                     {
                        id: 62, title: "大屏展示", path: "bigscreen/globalview",
                        children: [
                            { id: 621, title: '列表', code: 'index' }
                        ]
                    },
                ],
            },

            {
                id: 70,
                title: "用户管理",
                children: [
                    {
                        id: 71, title: "菜单管理",  path: "menu",
                        children: [
                            { id: 711, title: '列表', code: 'index' },
                            { id: 712, title: '保存', code: 'save' },
                            { id: 713, title: '更新', code: 'update' },
                            { id: 714, title: '删除', code: 'delete' },
                        ]
                    },
                    {
                        id: 72, title: "角色管理",  path: "role",
                        children: [
                            { id: 721, title: '列表', code: 'index' },
                            { id: 722, title: '保存', code: 'save' },
                            { id: 723, title: '更新', code: 'update' },
                            { id: 724, title: '删除', code: 'delete' },
                        ]
                    },
                    {
                        id: 73, title: "用户管理", path: "user",
                        children: [
                            { id: 731, title: '列表', code: 'index' },
                            { id: 732, title: '保存', code: 'save' },
                            { id: 733, title: '更新', code: 'update' },
                            { id: 734, title: '删除', code: 'delete' },
                        ]
                    },

                ],
            },


            {
                id: 80,
                title: "系统设置",
                children: [
                    {
                        id: 811111,
                        title: "基础数据",
                        children: [
                            {
                                id: 81, title: "教学楼管理", path: "building",
                                children: [
                                    { id: 811, title: '列表', code: 'index' },
                                    { id: 812, title: '保存', code: 'save' },
                                    { id: 813, title: '更新', code: 'update' },
                                    { id: 814, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 82, title: "楼层管理", path: "floor",
                                children: [
                                    { id: 821, title: '列表', code: 'index' },
                                    { id: 822, title: '保存', code: 'save' },
                                    { id: 823, title: '更新', code: 'update' },
                                    { id: 824, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 83, title: "教室管理", path: "room",
                                children: [
                                    { id: 831, title: '列表', code: 'index' },
                                    { id: 832, title: '保存', code: 'save' },
                                    { id: 833, title: '更新', code: 'update' },
                                    { id: 834, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 84, title: "数据维护", path: "init",
                                children: [
                                    { id: 841, title: '列表', code: 'index' },
                                    { id: 842, title: '保存', code: 'save' },
                                    { id: 843, title: '更新', code: 'update' },
                                    { id: 844, title: '删除', code: 'delete' },
                                ]
                            },


                        ]
                    },
                    {
                        id: 82333,
                        title: "一卡通管理",
                        children: [

                            {
                                id: 85, title: "一卡通管理",  path: "iccard",
                                children: [
                                    { id: 851, title: '列表', code: 'index' },
                                    { id: 852, title: '保存', code: 'save' },
                                    { id: 853, title: '更新', code: 'update' },
                                    { id: 854, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 86, title: "门锁管理",  path: "smartlock",
                                children: [
                                    { id: 861, title: '列表', code: 'index' },
                                    { id: 862, title: '保存', code: 'save' },
                                    { id: 863, title: '更新', code: 'update' },
                                    { id: 864, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 87, title: "开锁日志",  path: "smartlocklog",
                                children: [
                                    { id: 871, title: '列表', code: 'index' },
                                    { id: 872, title: '保存', code: 'save' },
                                    { id: 873, title: '更新', code: 'update' },
                                    { id: 874, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 88, title: "中控编程",  path: "program",
                                children: [
                                    { id: 881, title: '列表', code: 'index' },
                                    { id: 882, title: '保存', code: 'save' },
                                    { id: 883, title: '更新', code: 'update' },
                                    { id: 884, title: '删除', code: 'delete' },
                                ]
                            },
                            {
                                id: 89, title: "远程升级", controller: "system", action: "upgrade",
                                children: [
                                    { id: 891, title: '列表', code: 'index' },
                                    { id: 892, title: '保存', code: 'save' },
                                    { id: 893, title: '更新', code: 'update' },
                                    { id: 894, title: '删除', code: 'delete' },
                                ]
                            },

                        ],
                    },
                ],
            },


        ];
        return this.stdMessage(param._success, '数据错误', data);
    }

    // 显示指定的资源
    read(option) {
        let param = this.parseOption(option);
        console.log('[菜单模拟数据] 显示指定的资源 param=', param, '&option=', option);
        if (param.id == undefined) {
            return this.stdMessage(false, '参数无效');
        }
        let data = { id: 42, title: "角色管理", controller: "AuthRoles", action: "index", type: "1", status: "1", sort: 0 };
        return this.stdMessage(param._success, '错误', data);
    }

    // 保存新建的资源
    save(option) {
        let param = this.parseOption(option);
        console.log('[菜单模拟数据] 保存新建的资源 param=', param, '&option=', option);
        if (!param.user_name) {
            return this.stdMessage(false, '用户名是必填项');
        }
        return Mock.mock({
            success: param._success, // '@boolean'
            message: '@ctitle',
            code: 200,
            'data': {
                'id': '@id', // 随机编号
            }
        })
    }

    // 保存更新的资源
    update(option) {
        let param = this.parseOption(option);
        console.log('[菜单模拟数据] 保存更新的资源 param=', param, '&option=', option);
        if (!param.id || isNaN(param.id)) {
            return this.stdMessage(false, '参数无效');
        }
        if (!param.title || param.title.trim().length == 0) {
            return this.stdMessage(false, '名称无效');
        }
        return Mock.mock({
            success: param._success, // '@boolean'
            message: '@ctitle',
            code: 200,
            'data': {
                'id': param.id, // 随机编号
            }
        })

    }

    // 删除指定资源
    delete(option) {
        let param = this.parseOption(option);
        console.log('[菜单模拟数据] 删除指定资源 param=', param, '&option=', option);
        if (param.id == undefined) {
            return this.stdMessage(false, '参数无效');
        }
        return Mock.mock({
            success: param._success, // '@boolean'
            message: '@ctitle',
            code: 200,
            'data': {
                'id': param.id, // 随机编号
            }
        })
    }

    // 解析参数 option= {url:'/api/read?id=1', type:'GET', data:null}
    parseOption(option) {
        let param = {}
        if (option.url.indexOf('?') > -1) {
            new URLSearchParams(option.url.substring(option.url.indexOf('?') + 1)).forEach((v, k) => {
                param[k] = v;
            })
        }
        if (option.body) {
            if (option.body instanceof FormData) {
                option.body.forEach((v, k) => {
                    param[k] = v;
                })
            } else {
                param = JSON.parse(option.body);
            }
        }
        // 可以通过传参 _success 来控制返回消息的成功状态。默认返回成功消息。
        if (typeof (param._success) != 'boolean') {
            param._success = true;
        }
        return param;
    }

    // 返回标准消息对象
    stdMessage(success, message, data = '', code = 0) {
        return {
            success: success,
            message: message,
            data: data,
            code: code,
        }
    }
}
export default new MenusMock;